mod common;
use crate::common::*;
use crate::task::{ck_task, task_data};
use base::base::VMap;
use serde_json::json;

#[tokio::test]
async fn test_task_1() {
    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
                "sn": "",
            }
        }),
        "task",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
            }
        }),
        "task",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "task1",
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
            }
        }),
        "task",
        "add",
        json!({"code": 2503, "fail": true, "success": false, "message": "tech error: \"no tech\""}),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "tech",
        "add",
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "task1",
                "pstart": "2024-01-0101:01:01",
                "pend": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
            }
        }),
        "task",
        "add",
        e_para("pstart format error"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "task1",
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-0101:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
            }
        }),
        "task",
        "add",
        e_para("pend format error"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "task1",
                "pend": "2024-01-01 01:01:01",
                "pstart": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 3,
                "state": "open",
            }
        }),
        "task",
        "add",
        e_para("pend must gt pstart"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "task1",
                "pstart": "2024-01-01 01:01:01",
                "pend": "2024-02-01 01:01:01",
                "tech": "te1",
                "pnum": 0,
                "state": "open",
            }
        }),
        "task",
        "add",
        e_para("pnum must gt 0"),
    )
    .await;
}

/*  1. no any data
 *  2. add one task
 *  3. get all task
 *  4. get the added task item
 *  5. rm the task item
 *  6. get all task
 *  7. get the removed task item
 *  8. rm the task item again
 */
#[tokio::test]
async fn test_task_2() {
    let val = task_data();

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "task",
        "list",
        e_miss_s("s"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task1").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1").unwrap(),
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "s": "2024-01-01 09:09:09",
            "e": "2024-01-01 09:09:09",
        }),
        "task",
        "list",
        json!([val.ck_val("task1").unwrap(),]),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "get",
        val.ck_val("task1").unwrap().clone(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "rm",
    )
    .await;

    ck_task(pos!(), json!({})).await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "s": "2024-01-01 09:09:09",
            "e": "2024-01-01 09:09:09",
        }),
        "task",
        "list",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "get",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "rm",
    )
    .await;
}

/*  1. add one task
 *  2. modify pstart
 *  3. add close task
 *  4. modify pstart
 */
#[tokio::test]
async fn test_task_3() {
    let val = task_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task1").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pstart": "2024-01-01 02:01:01",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1__4112").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_4112").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task2").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run2").unwrap(),
                val.ck_val("run1__4112").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t").unwrap(),
            "task1": val.ck_val("task1_4112").unwrap(),
            "task2": val.ck_val("task2").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task2",
            "v": {
                "pstart": "2024-01-01 03:01:01",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1__4112").unwrap(),
                val.ck_val("run2_4113").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t").unwrap(),
            "task1": val.ck_val("task1_4112").unwrap(),
            "task2": val.ck_val("task2_4113").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task2",
            "v": {
                "state": "close",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1__4112").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t").unwrap(),
            "task1": val.ck_val("task1_4112").unwrap(),
            "task2": val.ck_val("task2_c4113").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task2",
            "v": {
                "state": "open",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1__4112").unwrap(),
                val.ck_val("run2_4113").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t").unwrap(),
            "task1": val.ck_val("task1_4112").unwrap(),
            "task2": val.ck_val("task2_4113").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "rm",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run2_4113").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t2").unwrap(),
            "task2": val.ck_val("task2_4113").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task2",
        }),
        "task",
        "rm",
    )
    .await;

    ck_task(pos!(), json!({})).await;
}

/*  1. add one task
 *  2. get the task
 *  3. modify one task
 *  4. get the task
 *  5. modify task ck
 *  6. remove the task
 *  7. get all task
 */
#[tokio::test]
async fn test_task_4() {
    let val = task_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task1").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pnum": 4,
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_n").unwrap(),
        }),
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "get",
        val.ck_val("task1_n").unwrap().clone(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "state": "close",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_nc").unwrap(),
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "tech": "ae1",
            }
        }),
        "task",
        "md",
        e_node(),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pstart": "2024-01-0101:01:01",
            }
        }),
        "task",
        "md",
        e_para("pstart format error"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pend": "2024-02-0101:01:01",
            }
        }),
        "task",
        "md",
        e_para("pend format error"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pend": "2024-01-01 01:01:01",
                "pstart": "2024-02-01 01:01:01",
            }
        }),
        "task",
        "md",
        e_para("pend must gt pstart"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pnum": 0,
            }
        }),
        "task",
        "md",
        e_para("pnum must gt 0"),
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_nc").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pstart": "2024-01-03 01:01:01",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d13").unwrap(),
            "d413": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_nc_413").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pstart": "2024-02-03 01:01:01",
                "pend": "2026-02-03 01:01:01",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m12").unwrap(),
            "m41": val.ck_val("d13").unwrap(),
            "m42": val.ck_val("d3").unwrap(),
            "d423": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_nc_423").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": {
                "pstart": "2025-02-03 01:01:01",
            }
        }),
        "task",
        "md",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y").unwrap(),
            "y4": val.ck_val("m12").unwrap(),
            "y5": val.ck_val("m2").unwrap(),
            "m52": val.ck_val("d3").unwrap(),
            "m41": val.ck_val("d13").unwrap(),
            "m42": val.ck_val("d3").unwrap(),
            "d523": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1_nc_523").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "rm",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m12").unwrap(),
            "m41": val.ck_val("d13").unwrap(),
            "m42": val.ck_val("d3").unwrap(),
        }),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "s": "2024-01-01 09:09:09",
            "e": "2024-01-01 09:09:09",
        }),
        "task",
        "list",
        e_empty("no data"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-01-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m2").unwrap(),
            "m42": val.ck_val("d3").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-01-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m2").unwrap(),
            "m42": val.ck_val("d3").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-02-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2025-02-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(pos!(), json!({})).await;
}

#[tokio::test]
async fn test_task_5() {
    let val = task_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task1").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task2_412").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task3").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task4").unwrap().clone()
        }),
        "task",
        "add",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
                val.ck_val("run2_412").unwrap(),
                val.ck_val("run3").unwrap(),
                val.ck_val("run4").unwrap(),
            ]),
            "all": val.ck_val("y").unwrap(),
            "y4": val.ck_val("m12").unwrap(),
            "y5": val.ck_val("m2").unwrap(),
            "m41": val.ck_val("d12").unwrap(),
            "m42": val.ck_val("d2").unwrap(),
            "m52": val.ck_val("d2").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "d412": val.ck_val("t2").unwrap(),
            "d422": json!(["task3"]),
            "d522": json!(["task4"]),
            "task1": val.ck_val("task1").unwrap(),
            "task2": val.ck_val("task2_412").unwrap(),
            "task3": val.ck_val("task3").unwrap(),
            "task4": val.ck_val("task4").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-01-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run3").unwrap(),
                val.ck_val("run4").unwrap(),
            ]),
            "all": val.ck_val("y").unwrap(),
            "y4": val.ck_val("m2").unwrap(),
            "y5": val.ck_val("m2").unwrap(),
            "m42": val.ck_val("d2").unwrap(),
            "m52": val.ck_val("d2").unwrap(),
            "d422": json!(["task3"]),
            "d522": json!(["task4"]),
            "task3": val.ck_val("task3").unwrap(),
            "task4": val.ck_val("task4").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-01-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-02-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run4").unwrap(),
            ]),
            "all": val.ck_val("y5").unwrap(),
            "y5": val.ck_val("m2").unwrap(),
            "m52": val.ck_val("d2").unwrap(),
            "d522": json!(["task4"]),
            "task4": val.ck_val("task4").unwrap(),
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2025-02-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_task(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "tech",
        "rm",
    )
    .await;
}
